package cn.wolfcode.service.impl;

import cn.wolfcode.domain.SeckillProduct;
import cn.wolfcode.domain.SeckillProductVo;
import cn.wolfcode.mapper.SeckillProductMapper;
import cn.wolfcode.redis.SeckillRedisKey;
import cn.wolfcode.service.ISeckillProductService;
import com.alibaba.fastjson.JSON;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;


@Service
public class SeckillProductServiceImpl implements ISeckillProductService {
    @Autowired
    private SeckillProductMapper seckillProductMapper;
    @Autowired
    private StringRedisTemplate redisTemplate;
    @Autowired
    private RocketMQTemplate rocketMQTemplate;


    @Override
    public SeckillProductVo find(Integer time, Long seckillId) {
        String key = SeckillRedisKey.SECKILL_PRODUCT_LIST.getRealKey(time.toString());
        Object voJson = redisTemplate.opsForHash().get(key, seckillId.toString());

        return JSON.parseObject(voJson.toString(), SeckillProductVo.class);
    }

    @Override
    public List<SeckillProductVo> queryByTime(Integer time) {
        List<Object> voJsonList = redisTemplate.opsForHash().values(SeckillRedisKey.SECKILL_PRODUCT_LIST.getRealKey(time.toString()));
        return voJsonList.stream().map(obj -> JSON.parseObject(obj.toString(), SeckillProductVo.class)).collect(Collectors.toList());
    }

    @Override
    public List<SeckillProduct> queryTime(Integer time) {
        return seckillProductMapper.queryCurrentlySeckillProduct(time);
    }
}
